/*
 *  CFile.mm
 *  Game_Demo
 *
 *  Created by Zhou XiaoHan on 11-2-24.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "CYQCFile.h"

using namespace cocos2d;

CYQCFile::CYQCFile()
{
//	fm = [[NSFileManager alloc]init];
//	file_data = nil;
	file_buff = NULL;
	m_pCur = NULL;
}

CYQCFile::~CYQCFile()
{
	if (file_buff != NULL)
	{
		delete file_buff;
	}
//	if (file_data != nil)
//	{
//		[file_data release];
//	}
//	if (fm != nil)
//	{
//		[fm release];
//	}
	if (m_pCur != NULL) //mustn't dealloc for memo leak
	{
		m_pCur = NULL;
		//delete m_pCur;
	}
}

void CYQCFile::divFileName(char* file_name, char* str_name, char* str_ext)
{

/*	char* str_point = new char(".");
	int index = file_name->indexOf(str_point);
	if (index == 0)//there's no ext name
	{
		str_name = file_name->subString(0, file_name->length());
	}
	else
	{
		str_name = file_name->subString(0, index);
		str_ext = file_name->subString(index + 1, file_name->length());
	}
	delete str_point; */
}


boolean CYQCFile::Open()
{
//	file_data = [fm contentsAtPath:@"a.bin"];
//	file_size = file_data.length;
//	file_buff = new signed char[file_size];
//	[file_data getBytes:file_buff length:file_size];
//	//		[file_data release];
//	//		file_data = nil;
//	
//	//		delete realPath;
//	m_pCur = file_buff;
	
//	FILE * file = fopen("/Users/zhouxiaohan/projects/Game_Demo/a.bin", "r");
//	fseek(file,0,SEEK_END);
//	int size = ftell(file);
//	fseek(file,0,SEEK_SET);
//	file_buff = new char[size];
//	
//	fgets(file_buff, size, file);
//	
//	fclose(file);
//	m_pCur = file_buff;
	return true;
}



boolean CYQCFile::Open( const string &strPath )
{
/**
	CCFileData data(strPath.c_str(), "r");
	
	unsigned long  nSize = data.getSize();
	unsigned char* pBuffer = data.getBuffer();

	if (! pBuffer)
	{
		return false;
	}


	char *m_pData = new char[nSize];

	memcpy(m_pData, pBuffer, nSize);

	m_pCur = m_pData; 
**/
//	fgets()
/**	unsigned long filesize=0;
	unsigned char* date; 


	date = CCFileUtils::getFileData(strPath.c_str(),"r",&filesize);

**/
	
	
/*	std::string pathKey = strPath;

	CCFileUtils::ccRemoveHDSuffixFromFile(pathKey);
	
	pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str());
	

	
	std::string fullpath = pathKey; // (CCFileUtils::fullPathFromRelativePath(path));
	
	FILE * file = fopen(fullpath.c_str(), "rb");
	
	if (file == NULL)
	{
		//CCLog("can't open file %@", strPath);
		//delete realPath;
		//delete str_temp;
		return false;
	}
	fseek(file,0,SEEK_END);
	file_size = ftell(file);
	fseek(file,0,SEEK_SET);
	file_buff = new char[file_size];


	int readsize = fread(file_buff, sizeof( char ), file_size, file);

	//fflush(file);
	fclose(file);


	m_pCur = file_buff;
	//delete realPath;
	//delete str_temp;
*/

	std::string pathKey = strPath;

	CCFileUtils::ccRemoveHDSuffixFromFile(pathKey);

	pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str());

	std::string fullpath = pathKey; // (CCFileUtils::fullPathFromRelativePath(path));

	unsigned long nSize = 0;
	
	unsigned char* pBuffer = CCFileUtils::getFileData(fullpath.c_str(), "rb", &nSize);
	
	m_pCur = pBuffer;


	return false;	
}



int CYQCFile::getLength()
{
	return file_size;
}


void CYQCFile::Skip(int size)
{
	m_pCur += size;
}

byte CYQCFile::ReadByte()
{
	return *m_pCur++;
}


void CYQCFile::ReadBytes(void* buf, int count)
{
	memcpy(buf, m_pCur, count);
	m_pCur += count;
}

void CYQCFile::ReadChars(void* buf,int count)
{
	memcpy(buf, m_pCur, count);
	m_pCur += count;
}

char CYQCFile::ReadChar(){

	return *m_pCur++;;
}


short CYQCFile::ReadShort()
{
	short r = *((short*)m_pCur);//(((m_pCur[1]&0xff)<<8)|(m_pCur[0]&0xff));
	m_pCur += 2;
	return r;
}

//for java's io format
short CYQCFile::ReadShortReverse()
{
	short r = (short)(((m_pCur[0]&0xff)<<8) | (m_pCur[1]&0xff));
	m_pCur += 2;
	return r;
}

int CYQCFile::ReadInt()
{
	int r;
	r = *(int*)m_pCur;
	m_pCur += 4;
	return r;
}

int CYQCFile::ReadIntReverse()
{
	int r;
	r = (int)(
			  ( (m_pCur[0]&0xff)<<24 )
			  | ( (m_pCur[1]&0xff)<<16)
			  | ( (m_pCur[2]&0xff) <<8)
			  | ( (m_pCur[3]&0xff))
			  );
	m_pCur += 4;
	return r;
}

long CYQCFile::ReadLong()
{
	long r;
	r = *(long*)m_pCur;
	m_pCur += 8;
	return r;
}


long CYQCFile::ReadLongReverse()
{
	long r;
	int byte_count = 8;
	for (int i = 0; i < byte_count; i++)
	{
		r |= (m_pCur[i] & 0xff) << (8 * (byte_count - i - 1));
	}
	m_pCur += 8;
	return r;		
}

int CYQCFile::ReadNumber(byte* b, int off, int num, boolean invert) {
	int result = 0;
	for (int i = 0; i < num; i++) {
		result <<= 8;

		int num = b[(off + i)];

		result |= num & 0xFF;
	}
	return result;
}




void CYQCFile::LoadGameRmsFile()
{
	/*std::string pathKey = GAMERMSFILE;
	CCFileUtils::ccRemoveHDSuffixFromFile(pathKey);

	pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str());


	std::string fullpath = pathKey; // (CCFileUtils::fullPathFromRelativePath(path));

	FILE * file = fopen(fullpath.c_str(), "rt");

	if (file == NULL)
	{
		file = fopen(fullpath.c_str(), "wt");
		if(file!=NULL)
		{
			CCLog("file Creat");
		}
	}*/
}

FILE* CYQCFile::getGameRmsFile()
{
	/*std::string pathKey = GAMERMSFILE;
	CCFileUtils::ccRemoveHDSuffixFromFile(pathKey);

	pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str());


	std::string fullpath = pathKey; // (CCFileUtils::fullPathFromRelativePath(path));

	FILE * file = fopen(fullpath.c_str(), "w+");

	return file;*/
	return NULL;
}